package org.dromara.oce.controller.inner;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.oce.domain.OceAdvertiserEverydayCost;
import org.dromara.oce.domain.bo.OceAdvertiserEverydayCostBo;
import org.dromara.oce.mapper.OceAdvertiserEverydayCostMapper;
import org.dromara.oce.service.impl.OceAdvertiserEverydayCostServiceImpl;
import org.dromara.oce.utils.CommonConstants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 短剧
 * @desc:
 * @author: hhl
 * @date：2025/6/24 18:25
 * @module 对内接口
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
@RequestMapping("/inner/drama")
public class OceDramaInnerController {

    private final OceAdvertiserEverydayCostMapper advertiserEverydayCostMapper;

    /**
     * 获取短剧相关数据
     */
    @GetMapping("/getData")
    public R<Map<String,Object>> getData(OceAdvertiserEverydayCostBo bo) {
        Map<String,Object> map = new HashMap<>();
        LambdaQueryWrapper<OceAdvertiserEverydayCost> lqw = SpringUtils.getBean(OceAdvertiserEverydayCostServiceImpl.class).buildQueryWrapper(bo);
        List<OceAdvertiserEverydayCost> dtos = advertiserEverydayCostMapper.selectJobList(lqw);
        if(CollectionUtils.isNotEmpty(dtos)){
            if (bo.getIaaFlag() != null) {
                if(bo.getIaaFlag().equals(CommonConstants.IAA_FLAG_NO)){
                    // 消耗
                    BigDecimal cost = dtos.stream().map(OceAdvertiserEverydayCost::getCost).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.ONE, 2, RoundingMode.HALF_UP);
                    // 当日充值
                    BigDecimal thatdayTotalAmount = dtos.stream().map(OceAdvertiserEverydayCost::getThatdayTotalAmount).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.ONE, 2, RoundingMode.HALF_UP);
                    // 订单数
                    Integer gamePayCount = dtos.stream().map(OceAdvertiserEverydayCost::getThatdayPaidTotalNum).filter(Objects::nonNull).reduce(0, Integer::sum);
                    map.put("payCount", gamePayCount);
                    map.put("totalAmount", thatdayTotalAmount);
                    map.put("cost", cost);
                }else{
                    // 消耗
                    BigDecimal cost = dtos.stream().map(OceAdvertiserEverydayCost::getCost).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.ONE, 2, RoundingMode.HALF_UP);
                    // 广告收入
                    BigDecimal iaaTotalAmount = dtos.stream().map(OceAdvertiserEverydayCost::getIaaTotalAmount).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.ONE, 2, RoundingMode.HALF_UP);
                    map.put("totalAmount", iaaTotalAmount);
                    map.put("cost", cost);
                }
            }
        }
        return R.ok(map);
    }


}
